﻿using System;
using System.Data.SQLite;
using System.Windows.Forms;

namespace CSharp大作业
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            ReadScores();
        }

        public void ReadScores()
        {
            string displayText = "名字\t成绩\t总雷数\r\n";
            using (var conn = new SQLiteConnection("Data Source=Storage\\minesweeper.db;Version=3;"))
            {
                conn.Open();
                var cmd = new SQLiteCommand("SELECT Name, Score, Mines FROM Leaderboard ORDER BY Score DESC", conn);
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        displayText += $"{reader["Name"]}\t{reader["Score"]}\t{reader["Mines"]}\r\n";
                    }
                }
            }
            textBox1.Text = displayText;
        }

        private void button1_Click(object sender, EventArgs e) // 关闭
        {
            this.Close();
        }

        private void reset_Click(object sender, EventArgs e) // 重置按钮
        {
            using (var conn = new SQLiteConnection("Data Source=Storage\\minesweeper.db;Version=3;"))
            {
                conn.Open();
                var cmd = new SQLiteCommand("DELETE FROM Leaderboard", conn);
                cmd.ExecuteNonQuery();
            }
            ReadScores();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

